<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    (function () {
        var timeouts = [];
        var messageName = 'zero-timeout-message';
        //? 保持 setTimeout 的形态，只接受单个函数的参数，延迟始终为 0。
        function setZeroTimeout(fn) {
            timeouts.push(fn);
            window.postMessage(messageName, '*');
        }
        function handleMessage(event) {
            if (event.source == window && event.data == messageName) {
                event.stopPropagation();
                if (timeouts.length > 0) {
                    var fn = timeouts.shift();
                    fn();
                }
            }
        }
        window.addEventListener('message', handleMessage, true);
        //? 把 API 添加到 window 对象上
        window.setZeroTimeout = setZeroTimeout;
    })();
    var a = performance.now();
    setZeroTimeout(() => {
        let b = performance.now();
        console.log(b - a);
        setZeroTimeout(() => {
            let c = performance.now();
            console.log(c - b);
            setZeroTimeout(() => {
                let d = performance.now();
                console.log(d - c);
                setZeroTimeout(() => {
                    let e = performance.now();
                    console.log(e - d);
                    setZeroTimeout(() => {
                        let f = performance.now();
                        console.log(f - e);
                        setZeroTimeout(() => {
                            let g = performance.now();
                            console.log(g - f);
                        }, 0);
                    }, 0);
                }, 0);
            }, 0);
        }, 0);
    }, 0);

</script>
</body>
</html>
